#!/usr/bin/env python2
#
# Copyright (c) 2019 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from string import Template

from jvpp_model import EnumSet


def generate_enumsets(work_dir, model, logger):
    logger.debug("Generating enumset for %s " % model.json_api_files)

    for t in model.types:
        if not isinstance(t, EnumSet):
            continue
        logger.debug("Generating DTO for enumset %s", t)
        type_class_name = t.java_name
        type_class = _ENUM_TEMPLATE.substitute(
            plugin_package=model.plugin_package,
            c_type_name=t.name,
            json_filename=model.json_api_files,
            json_definition=t.doc,
            java_name=type_class_name,
            java_enum_name=type_class_name + "Options",
            constants=_generate_constants(t.constants, t.value.type.java_name),
            value_type=t.value.type.java_name
        )
        with open("%s/%s.java" % (work_dir, type_class_name), "w") as f:
            f.write(type_class)


_ENUM_TEMPLATE = Template("""
package $plugin_package.types;

import java.util.EnumSet;

/**
 * <p>This class represents $c_type_name enumset definition.
 * <br>It was generated by enumsets_gen.py based on $json_filename:
 * <pre>
$json_definition
 * </pre>
 */
public final class $java_name {

    private final EnumSet<$java_enum_name> options;

    public $java_name() {
        options = EnumSet.noneOf($java_enum_name.class);
        setOptionsValue(0);
    }

    public $java_name($value_type value) {
        options = EnumSet.noneOf($java_enum_name.class);
        setOptionsValue(value);
    }

    @Override
    public boolean equals(final Object obj) {
        if (!( obj instanceof $java_name)) {
            return false;
        }
        return options.equals((($java_name)obj).getOptions());
    }

    public EnumSet<$java_enum_name> getOptions() {
        return options;
    }

    public $value_type getOptionsValue() {
        $value_type optionsValue = 0;
        for ($java_enum_name opts : options) {
            optionsValue = ($value_type) (optionsValue | opts.value);
        }
        return optionsValue;
    }

    public void setOptionsValue(int value) {
        options.clear();

        if (value == 0) {
            // if value is "0" set default value and exit
            options.add($java_enum_name.forValue(($value_type)0));
            return;
        }

        for ($java_enum_name option : $java_enum_name.values()) {
            if ((option.value > 0) & ((option.value & value) == option.value)) {
                options.add(option);
            }
        }
    }

    public boolean add($java_enum_name option) {
        if (option.value == 0) return false;

        if (getOptionsValue() == 0) {
            options.clear();
        }
        return options.add(option);
    }

    public boolean remove($java_enum_name option) {
        if (option.value == 0) return false;

        options.remove(option);
        if (options.isEmpty()) {
            clear();
        }
        return true;
    }

    public boolean removeAll(EnumSet<$java_enum_name> optionsToRemove) {
        boolean retVal = options.removeAll(optionsToRemove);
        if (options.isEmpty()) {
            clear();
            retVal = true;
        }
        return retVal;
    }

    public void clear() {
        setOptionsValue(0);
    }

    public boolean contains($java_enum_name option) {
        return options.contains(option);
    }

    public boolean containsAll(EnumSet<$java_enum_name> options) {
        return options.containsAll(options);
    }

    @Override
    public java.lang.String toString() {
        return "$java_name{" + "options=" + options + '}';
    }

    public enum $java_enum_name {
$constants;

        public final $value_type value;

        $java_enum_name(final $value_type value) {
            this.value = value;
        }

        public static $java_enum_name forValue(final $value_type value) {
            for ($java_enum_name enumeration : $java_enum_name.values()) {
                if (value == enumeration.value) {
                    return enumeration;
                }
            }
            return $java_enum_name.values()[0];
        }
    }
}
""")


def _generate_constants(constants, type_name):
    return ",\n".join(_CONSTANT_TEMPLATE.substitute(name=c['name'], value=c['value'],
                                                    value_type=type_name) for c in constants)


_CONSTANT_TEMPLATE = Template("""        $name(($value_type)$value)""")
